package com.cbd.controller;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.cbd.pojo.Landlady;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.Personalcarport;
import com.cbd.service.PersonalcarportService;
import com.cbd.service.PurposeService;
import com.cbd.service.RentInfoService;
import com.cbd.service.impl.PersonalcarportServiceImpl;
import com.cbd.util.IQuantity;

@Controller
public class PersonalCarPortContorller {
	@Resource(name="personalcarportServiceImpl")
	private PersonalcarportService pls;
	@Resource(name="rentInfoServiceImpl")
	private RentInfoService rs;
	@Resource(name="purposeServiceImpl")
	private PurposeService purposeService;
	@RequestMapping(path="cbd/landlady/findOnePageCarPort")
	public@ResponseBody PageBean findAllCarPortByLadyId(@RequestParam("id") String id,@RequestParam("currentPage")String currentPage, @RequestParam("pageSize")String pageSize){
		PageBean page = new PageBean();
		page.setCurPage(Integer.parseInt(currentPage));
		page.setPageSize(IQuantity.pageSize);
		page = pls.findAllCarPortByLadyId(Integer.parseInt(id), page);
		List<Personalcarport> list =(List<Personalcarport>) page.getList();
		for (Personalcarport p : list) {
			p.setLandlady(null);
		}
		return page;
		
	}
	@RequestMapping(path="cbd/landlady/delCarPort")
	public void delCarPortById(String id,PrintWriter pw){
		System.out.println(id);
		pls.delCarPortById(Integer.parseInt(id));
		pw.print("");
	}
	@RequestMapping(path="cbd/landlady/addCarPort")
	public String addCarPort(@Valid Personalcarport personalcarport,HttpServletRequest req,BindingResult br,@RequestParam(value="myFile",required=false)CommonsMultipartFile file ){
		String id = req.getParameter("ladyID");
		Landlady landlady = new Landlady();
		landlady.setLadyID(Integer.parseInt(id));
		personalcarport.setLandlady(landlady);
		Date d = new Date();
		personalcarport.setApplyTime(d);
		String fileName="";
		if(!file.isEmpty()){
			fileName = changeFileName(file.getOriginalFilename());
		}
	
		String path=req.getSession().getServletContext().getRealPath("upload");
		try {
			file.getFileItem().write(new File(path + File.separator + fileName));
			personalcarport.setCarportPic("upload"+ File.separator + fileName);
			System.out.println(personalcarport.getCarportPic());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(br.hasErrors()){
			System.out.println(br.getAllErrors().get(0));
			return "redirect:addCarPort.jsp";
		}
		pls.addPort(personalcarport);
		return "redirect:myCarPort.jsp";
		
	}
	@RequestMapping(path="cbd/landlady/checkCarportNO")
	public @ResponseBody int checkCarportNO(String carportNO){
		int num  = pls.checkCarportNO(carportNO);
		return num;
		
	}
	@RequestMapping(path="cbd/landlady/checkCarNO")
	public @ResponseBody int checkCarNO(String carNO,String carportAddress){
		int num = pls.checkCarNO(carNO, carportAddress);
		return num;
		
	}
	@RequestMapping(path="cbd/landlady/getAllCarPort")
	public@ResponseBody List<Personalcarport> getAllCarPort(String id){
		List<Personalcarport> list = pls.getAllCarPort(Integer.parseInt(id));
		for (Personalcarport p : list) {
			p.setLandlady(null);
		}
		return list;
		
	}
	@RequestMapping(path="cbd/landlady/checkIspublish")
	public @ResponseBody int checkIspublish(String id){
		int num = rs.checkIsPublish(Integer.parseInt(id));
		return num;
		
	}
	@RequestMapping(path="cbd/landlady/checkHasPurpose")
	public @ResponseBody int checkHasPurpose(String id){
		int num  = purposeService.checkHasPurpose(Integer.parseInt(id));
		return num;
		
	}
	public String changeFileName(String fileName){
		long sys = System.currentTimeMillis();
		String newName = sys+"."+fileName.split("[.]")[1];
		return newName;
	}
	@RequestMapping(path="cbd/landlady/checkAllPort")
	public void checkAllPort(PrintWriter writer){
		//pls.checkAllPort();
			pls.checkAllPort();
			writer.print("");
	}
}
